//
//  MDHRippleAnimationImageView.swift
//  MDHPT
//
//  Created by 高得华 on 2019/8/4.
//  Copyright © 2019年 高得华. All rights reserved.
//

import UIKit

open class MDHRippleAnimationImageView: UIView {

    //MARK: - ==== 属性
    ///动画的layer
    fileprivate var _animationLayer: CALayer?
    ///是否在执行动画中
    fileprivate var _isAnimating = false
    ///动画组
    fileprivate var  _animationGroup: CAAnimationGroup?
    ///当前的图片
    @objc open lazy var imageView: UIImageView = {
        let imv = UIImageView()
        self.addSubview(imv)
        return imv
    }()
    ///cornerRadius///圆角
    @objc public var radiusCorner: CGFloat = 0
    ///总共有多少个圈
    @objc public var countPulsing = 5
    
    override open func layoutSubviews() {
        super.layoutSubviews()
        self.imageView.frame = self.bounds
        self.imageView.dh_cornerRadius = self.radiusCorner
    }
}

extension MDHRippleAnimationImageView {
    ///开始动画
    @objc open func startCustomAnimation() -> () {
        if _isAnimating {
            return
        }
        let animationLayer = CALayer()
        let pulsingLayers = self.setupAnimationLayers(rect: self.frame)
        for layer in pulsingLayers {
            animationLayer.addSublayer(layer)
        }
        self.layer.addSublayer(animationLayer)
        _animationLayer = animationLayer
        _isAnimating = true
    }
    
    ///结束动画
    @objc open func stopCustomAnimation() -> () {
        if _isAnimating {
            let pulsingLayers = _animationLayer?.sublayers ?? [CALayer]()
            for layer in pulsingLayers {
                layer.removeAllAnimations()
                layer.removeFromSuperlayer()
            }
            _animationLayer?.removeAllAnimations()
            _animationLayer?.removeFromSuperlayer()
            _isAnimating = false
        }
    }
    
    ///创建动画
    fileprivate func setupAnimationLayers(rect: CGRect) -> [CALayer] {
        var ret = [CALayer]()
        ///一次多少圈 的 layer
        let pulsingCount = self.countPulsing
        ///一圈d执行动画时间
        let animationDuration: Double = 3
        for i in 0..<pulsingCount {
            let pulsingLayer = CALayer()
            pulsingLayer.frame = CGRect.init(x: 0, y: 0, width: rect.width, height: rect.height)
            pulsingLayer.borderColor = UIColor.color(hex: "#35A4FF").cgColor
            pulsingLayer.borderWidth = 1
            pulsingLayer.cornerRadius = self.radiusCorner//8//rect.height/2.0
            
            ///
            let defaultCurve = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.default)
            
            let animationGroup = CAAnimationGroup()
            animationGroup.fillMode = CAMediaTimingFillMode.backwards
            animationGroup.beginTime = CACurrentMediaTime()+Double(i)*animationDuration/Double(pulsingCount)
            animationGroup.duration = animationDuration
            animationGroup.repeatCount = HUGE
            animationGroup.timingFunction = defaultCurve
            
            let scaleAnimation = CABasicAnimation.init(keyPath: "transform.scale")
            scaleAnimation.fromValue = 1.0
            scaleAnimation.toValue   = 1.36
            
            var oaValues = [NSNumber]()
            var oaTimes  = [NSNumber]()
            let count = pulsingCount*2
            for i in 0...count {
                let value = Float(i/count)
                oaValues.append(NSNumber.init(value: 1-value))
                oaTimes.append(NSNumber.init(value: value))
            }
            let opacityAnimation = CAKeyframeAnimation.init(keyPath: "opacity")
            opacityAnimation.values = oaValues
            opacityAnimation.keyTimes = oaTimes
            
            animationGroup.animations = [scaleAnimation, opacityAnimation]
            pulsingLayer.add(animationGroup, forKey: "plulsing")
            
            ret.append(pulsingLayer)
        }
        return ret
    }
}
